import { useReducer } from 'react';

/**
 * 定义初始状态信息
 */
export interface IstateType {
  selectType: any; // 入库类型 材料/资产
  assetTypeInfo: any; // 资产入库选择的资产类型
  materielList: any;
  selectFormInfo: any;
  selectAsssetInfo: any;
  selectApplyStorageList: any;
  selectRecordId: any;
}

/**
 * 新增和编辑页面操作枚举
 * @CLEAR_RECEIVE  清空存入 数据
 * @SELECT_MATERIEL 选中的材料数据列表
 *
 */
export enum IAcquisitionActionEnum {
  CLEAR_RECEIVE = 'clearAllReceive',
  SELECT_MATERIEL = 'selectMateriel', // 选择的材料/资产信息
  CANCEL_MATERIEL = 'cancelMateriel', // 取消选择的材料/资产信息
  SELECT_RECORD_ID = 'selectRecordId', // 选择的当前行数据id
  SELECT_FORM_INFO = 'selectFormInfo', // 选择的form数据
  CANCEL_FORM_INFO = 'cancelFormInfo', // 删除选择的form数据
  SELECT_TYPE = 'selectType', // 选择的入库类型
  SELECT_ASSET_INFO = 'selectAsssetInfo', // 选择的资产材料信息
  SELECT_ASSET_TYPE = 'selectAssetType', // 选择的资产类型
  SELECT_APPLY_STORAGE = 'selectApplyStorage', // 选择的到货验收
  CHANGE_MATERIALTYPE = 'CHANGE_MATERIALTYPE',
}

/**
 * 定义新增和编辑页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IAcquisitionActionEnum;
  payload?: any;
}

export const useAcquisitionManageReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    materielList: [],
    selectFormInfo: {},
    selectType: '',
    assetTypeInfo: {},
    selectAsssetInfo: {},
    selectApplyStorageList: [],
    selectRecordId: '',
  };

  const acquisitionManageReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /** 选中的材料列表 */
      case IAcquisitionActionEnum.SELECT_MATERIEL:
        return {
          ...state,
          materielList: action?.payload,
        };
      /** 取消选中的材料列表中的某项 */
      case IAcquisitionActionEnum.CANCEL_MATERIEL:
        return {
          ...state,
          materielList: [],
        };
      /** 选中的form表单数据 */
      case IAcquisitionActionEnum.SELECT_FORM_INFO:
        return {
          ...state,
          selectFormInfo: action?.payload,
        };
      // 选择的当前行数据id
      case IAcquisitionActionEnum.SELECT_RECORD_ID:
        return {
          ...state,
          selectRecordId: action?.payload,
        };
      /** 取消选中的form表单数据 */
      case IAcquisitionActionEnum.CANCEL_FORM_INFO:
        return {
          ...state,
          selectFormInfo: {},
        };
      /** 选择的入库类型 */
      case IAcquisitionActionEnum.SELECT_TYPE:
        return {
          ...state,
          selectType: action?.payload,
        };
      /** 选择的资产材料信息*/
      case IAcquisitionActionEnum.SELECT_ASSET_INFO:
        return {
          ...state,
          selectAsssetInfo: action?.payload,
        };
      /** 选择的资产材料信息*/
      case IAcquisitionActionEnum.SELECT_ASSET_TYPE:
        return {
          ...state,
          assetTypeInfo: action?.payload,
        };
      // 选择的到货验收list
      case IAcquisitionActionEnum.SELECT_APPLY_STORAGE:
        return {
          ...state,
          selectApplyStorageList: action?.payload,
        };
      //
      case IAcquisitionActionEnum.CHANGE_MATERIALTYPE:
        return {
          ...state,
          selectType: action?.payload,
          materielList: [],
        };
      /**
       *  清空已选数据
       */
      case IAcquisitionActionEnum.CLEAR_RECEIVE:
        return {
          ...state,
          leftSelectRows: [],
          leftSelectKeys: [],
          leftExpandedKeys: [],
          leftAutoExpandParent: false,
          leftSelectName: [],
        };

      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, acquisitionDispatch] = useReducer(acquisitionManageReducer, initState);

  return {
    ...state,
    acquisitionDispatch,
  };
};
